//
//  CTPopMenuTableViewCell.m
//  IBDP
//
//  Created by cattsoft on 2017/7/14.
//  Copyright © 2017年 cattsoft. All rights reserved.
//

#import "CTPopMenuTableViewCell.h"

@interface CTPopMenuTableViewCell ()

@property (nonatomic,strong) UIImageView *iconImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation CTPopMenuTableViewCell

- (instancetype)initWithTableView:(UITableView *)tableView popMenuModel:(CTPopMenuModel *)cellModel style:(CTPopMenuStyle *)menuStyle {
    static NSString *identifier = @"CTPopMenuTableViewCell";
    CTPopMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CTPopMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.titleLabel.textColor = menuStyle.titleColor;
    cell.titleLabel.font = menuStyle.titleFont;
    cell.iconImgView.image = [UIImage imageNamed:cellModel.imageName];
    cell.titleLabel.text = cellModel.title;
    [cell setupUICellModel:cellModel menuStyle:menuStyle];
    return cell;
}

- (void)setupUICellModel:(CTPopMenuModel *)cellModel menuStyle:(CTPopMenuStyle *)menuStyle {
    if ([cellModel.imageName isEqualToString:@""] || cellModel.imageName == nil) {
        self.iconImgView.frame = CGRectMake(menuStyle.iconFrameX, 0.5*(menuStyle.cellHeight-20), 0, 20);
    }else {
        self.iconImgView.frame = CGRectMake(menuStyle.iconFrameX, (menuStyle.cellHeight-20)*0.5, 20, 20);
    }
    self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImgView.frame)+5, 0, self.bounds.size.width-(CGRectGetMaxX(self.iconImgView.frame)+5), menuStyle.cellHeight);
}

- (UIImageView *)iconImgView {
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        [self.contentView addSubview:_iconImgView];
    }
    return _iconImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.iconImgView.frame)+5, 0, self.bounds.size.width-(CGRectGetMaxX(self.iconImgView.frame)+5), self.bounds.size.height)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 1;
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
